<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/admin/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form layuimini-form" action="" lay-filter="edit">
            <div class="layui-form-item">
                <label class="layui-form-label required">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="user" lay-verify="required" lay-reqtext="用户名是必填项"
                           placeholder="请输入用户名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="required" lay-reqtext="姓名是必填项"
                           placeholder="请输入姓名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="0" title="未知" checked="">
                    <input type="radio" name="sex" value="1" title="男">
                    <input type="radio" name="sex" value="2" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">手机号</label>
                <div class="layui-input-inline">
                    <input type="text" name="mobile" lay-verify="required|phone" lay-reqtext="手机号是必填项"
                           placeholder="请输入手机号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="state" lay-skin="switch" lay-filter="switchState" lay-text="启用|禁用">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色</label>
                <div class="layui-input-inline">
                    <input type="text" name="role" placeholder="" autocomplete="off" class="layui-input" id="role"
                           value="" ts-selected="">
                    <input type="hidden" name="roles" id="roles">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="/static/admin/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/admin/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer', 'tableSelect'], function () {
        var form = layui.form,
            layer = layui.layer,
            tableSelect = layui.tableSelect,
            $ = layui.$;

        //监听指定开关
        form.on('switch(switchState)', function (data) {
            if (this.checked) {
                this.value = 0
            } else {
                this.value = 1
            }
        });
        //监听提交
        form.on('submit(submit)', function (data) {
            data.field.state = data.field.state ? 0 : 1
            let id = "${.info.Id}"
            let method = 'post'
            if (id > 0) {
                method = 'put'
                data.field.id = id
            }
            $.ajax({
                url: "/api/system/admin",
                type: method,
                data: data.field,
                success: function (res) {
                    layer.msg(res.msg);
                    if (res.code === 0) {
                        parent.location.reload(); //成功后再刷新
                    }
                },
                error: function (res) {
                    layer.msg(res.msg);
                    return false
                }
            })
            return false;
        });

        tableSelect.render({
            elem: '#role',
            searchKey: 'name',
            checkedKey: 'id',
            width: '200',
            searchPlaceholder: '搜索角色名',
            table: {
                url: '/api/system/role',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'ID', width: 100},
                    {field: 'name', title: '姓名'},
                ]]
            },
            done: function (elem, data) {
                var NEWJSON = []
                var roles = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.name)
                    roles.push(item.id)
                })
                elem.val(NEWJSON.join(","))
                document.getElementById("roles").value = roles.join(",")
            }
        })

        //表单初始赋值
        form.val('edit', {
            "user": "${.info.User}"
            , "name": "${.info.Name}"
            , "mobile": "${.info.Mobile}"
            , "sex": "${.info.Sex}"
            , "state": "${if not .info.State}true${end}"
            , "remark": "${.info.Remark}"
        })
        $('#role').attr('ts-selected', "${.info.Roles}").attr("value", "${.role}")
        $('#roles').attr('value', "${.info.Roles}")
    });
</script>

</body>
</html>